package com.web.test.dao;

import com.web.test.bean.Grade;
import com.web.test.bean.Middle;
import com.web.test.utils.JDBCUtil;

import java.io.IOException;
import java.sql.*;

/**
 * Created by Administrator on 2017/9/23.
 */
public class GradeDao {

    // execute()返回boolean值，如果是查询返回true，如果是更新false
    public boolean insert(Grade grade) throws ClassNotFoundException,
            SQLException, IOException {

        Connection connection = JDBCUtil.getConnection();
        String sql = "insert into STU_GRADE values(?,?,?)";
        PreparedStatement pStatement = connection.prepareStatement(sql);

        pStatement.setString(1, "");
        pStatement.setString(2, grade.getStuNo());
        pStatement.setInt(3, grade.getGrade());

        if (!pStatement.execute()) {
            JDBCUtil.close(connection, pStatement, null);
            return true;
        }

        return false;

    }

    public int getMaxGrade(String stuNo) throws SQLException, IOException, ClassNotFoundException {

        Connection connection = JDBCUtil.getConnection();
        String sql = "SELECT max(g.STU_GRADE) g_max FROM STU_GRADE g WHERE stu_no='"+stuNo+"'";
        Statement pStatement = connection.createStatement();

        ResultSet resultSet = pStatement.executeQuery(sql);


        int max = 0;
        while (resultSet.next()) {

            max = resultSet.getInt("g_max");
        }

        JDBCUtil.close(connection, pStatement, null);


        return max;

    }

}
